package com.example.d3.character.compare;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;

public class CharacterSelectionActivity extends ListActivity {
	private HeroData[] m_heroData;
	private String lastURL = "";
	private HeroData selectedHero;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.character_list);
        this.asyncJson();
    }
    
    public void asyncJson(){
        AQuery aq = new AQuery(this);
        Intent intent = getIntent();
        String btName = intent.getStringExtra(AccountSelectionActivity.battletag_name);
        String btCode = intent.getStringExtra(AccountSelectionActivity.battletag_code);
        lastURL = "http://us.battle.net/api/d3/profile/" + btName + "-" + btCode + "/";
    	
    	aq.ajax(lastURL, JSONObject.class, this, "jsonCallback"); //http://us.battle.net/api/d3/profile/banished-1684/
    }
    
    public void jsonCallback(String url, JSONObject json, AjaxStatus status){    	
		TextView textView = new TextView(this);
        textView.setTextSize(40);
    	if(json != null){
    		try {
				JSONArray heroes = json.getJSONArray("heroes");
				m_heroData = new HeroData[heroes.length()];
				//generate list of heroes
				for(int i = 0; i < heroes.length(); i++){
					JSONObject hero = heroes.getJSONObject(i);
					HeroData hd = new HeroData(hero.getString("name"), hero.getInt("id"), hero.getInt("level"),
							hero.getInt("paragonLevel"), hero.getBoolean("hardcore"), hero.getBoolean("dead"),
							hero.getInt("gender"), hero.getString("class"), hero.getInt("last-updated"));

					m_heroData[i] = hd;
				}
			} catch (JSONException e) {
				textView.setText(e.toString());
				setContentView(textView);
				e.printStackTrace();
			}
    		
    	} else {
            textView.setText("Error: Unable to locate user");
        	setContentView(textView);
    	}
    	this.setListAdapter(new HeroDataAdapter(this, m_heroData));
    	
    	ListView lv = getListView();
    	lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
	              HeroData hd = selectedHero = m_heroData[pos];

	              D3CompareApp app = (D3CompareApp)getApplication();
	              if(app.getHero1().isValid() && app.getHero2().isValid()){
	            	  new AlertDialog.Builder(view.getContext())
	            	  	.setMessage("Which character do you want to replace?")
	            	  	.setPositiveButton(app.getHero1().name(), new DialogInterface.OnClickListener(){
	            	  		public void onClick(DialogInterface dialog, int id) {
	            	  			 D3CompareApp app = (D3CompareApp)getApplication();
	            	  			app.setHero1(selectedHero);
	            	  		}
	            	  	})
	            	  	.setNegativeButton(app.getHero2().name(), new DialogInterface.OnClickListener(){
	            	  		public void onClick(DialogInterface dialog, int id) {
	            	  			 D3CompareApp app = (D3CompareApp)getApplication();
	            	  			app.setHero2(selectedHero);
	            	  		}
	            	  	})
	            	  	.show();
	              }
	              int slot = app.setHero(hd);
	              // Launching new Activity on selecting single List Item
	              Intent i = new Intent(getApplicationContext(), Character.class);
	              
	              // Send character URL
	              i.putExtra("URL", lastURL);
	              i.putExtra("HeroNum", slot);
	              startActivity(i);
			}
        });
    }
}
